unit HUtils;

//========================================================================================
//
//  HUtils.pas
//
//  Calls:
//
//  Called By:
//
//  Ver: 1.0.0
//
//  Date: 20 Oct 2016
//
//========================================================================================

{$mode objfpc}{$H+}

interface

uses
  Classes, Dialogs, Forms, SysUtils;

// Character Validation Routines
function ValidAlphaCharacter( Key: char) : char;
function ValidAlphaNumericCharacter( Key: char) : char;
function ValidCallsignCharacter( Key: char) : char;
function ValidDigitCharacter( Key: char) : char;
function ValidFloatCharacter( Key: char) : char;
function ValidFilenameCharacter (Key: char) : char;
function ValidDirectoryCharacter (Key: char) : char;
function ValidPasswordCharacter (Key: char) : char;

// Valid String Routines
procedure ValidRealStr (vstrValue: string; var vblnValid : Boolean);

// Conversion Routines
function HUBoolToStr( vblnBoolValue: boolean): string;
function HUStrToBool( vstrBoolStr: string): boolean;

// Message Boxes
function ErrorMessageDlgOk(vstrCaption, vstrMsg  : string) : TModalResult;
function InfoMessageDlgOk(vstrCaption, vstrMsg  : string) : TModalResult;
function ConfirmationMessageDlg(vstrCaption, vstrMsg  : string) : TModalResult;

// Registration Routines
function CalculateRegistrationKey (vstrInputString : string) : string;

// Debugging Routines
procedure ShowInt(vstrTitle : string; vintValue : integer );
procedure ShowFloat(vstrTitle : string; vfltValue : double );

const
  keyNull = #0;
  keyBS = #8;
  keyTab = #9;
  keyEnter = #13;
  keySpace = #32;
  keyLArrow = #37;
  keyUArrow = #38;
  keyRArrow = #39;
  keyDArrow = #40;
  keyDecimalPoint = #46;
  keyPeriod = #46;
  keyFSlash = #47;
  keyDEL = #127;

  key0 = #48;
  key1 = #49;
  key2 = #50;
  key3 = #51;
  key4 = #52;
  key5 = #53;
  key6 = #54;
  key7 = #55;
  key8 = #56;
  key9 = #57;

  keyA = #65;
  keyB = #66;
  keyC = #67;
  keyD = #68;
  keyE = #69;
  keyF = #70;
  keyG = #71;
  keyH = #72;
  keyI = #73;
  keyJ = #74;
  keyK = #75;
  keyL = #76;
  keyM = #77;
  keyN = #78;
  keyO = #79;
  keyP = #80;
  keyQ = #81;
  keyR = #82;
  keyS = #83;
  keyT = #84;
  keyU = #85;
  keyV = #86;
  keyW = #87;
  keyX = #88;
  keyY = #89;
  keyZ = #90;

  keyUScore = #95;

  key_a = #97;
  key_b = #98;
  key_c = #99;
  key_d = #100;
  key_e = #101;
  key_f = #102;
  key_g = #103;
  key_h = #104;
  key_i = #105;
  key_j = #106;
  key_k = #107;
  key_l = #108;
  key_m = #109;
  key_n = #110;
  key_o = #111;
  key_p = #112;
  key_q = #113;
  key_r = #114;
  key_s = #115;
  key_t = #116;
  key_u = #117;
  key_v = #118;
  key_w = #119;
  key_x = #120;
  key_y = #121;
  key_z = #122;

  keySingleQuote = #222;

  // Colors
  clInvalidContents = $00A6A6FF;
  clValidContents = $0080FF80;

implementation

//========================================================================================
//          CHARACTER VALIDATION ROUTINES
//========================================================================================
function ValidAlphaCharacter( Key: char) : char;
begin
    // Returns only Valid Alphabetic Characters. Non-valid characters are converted
    // into Null (#0) characters.
    //Valid Alpha Characters are:
    // <BS>
    // <SP>
    // [A..Z]
    // [a..z]
    Result := Key;
    case Key of
      keyBS : Exit; // <BS>
      keySpace : Exit; // <SP>
      keyA..keyZ : Exit; // [A..Z]
      key_a..key_z : Exit; // [a..z]
    else
      Result := keyNull;
    end;// case Key of
end;// function ValidAlphaCharacter(var Key: char);

//========================================================================================
function ValidAlphaNumericCharacter( Key: char) : char;
begin
    // Returns only Valid Alphabetic Characters. Non-valid characters are converted
    // into Null (#0) characters.
    //Valid Alpha C haracters are:
    // <BS>
    // <SP>
    // [A..Z]
    // [a..z]
    // [0..9]
    Result := Key;
    case Key of
      keyBS : Exit; // <BS>
      keySpace : Exit; // <SP>
      key0..key9 : Exit; // [0..9]
      keyA..keyZ : Exit; // [A..Z]
      key_a..key_z : Exit; // [a..z]
    else
      Result := keyNull;
    end;// case Key of
end;// function ValidAlphaNumericCharacter(var Key: char);

//========================================================================================
function ValidCallsignCharacter( Key: char) : char;
begin
    // Returns only Valid Callsign Characters. Non-valid characters are converted
    // into Null (#0) characters.
    //Valid Alpha C haracters are:
    // <BS>
    // </>
    // [0..9]
    // [A..Z]
    // [a..z] Converted to Uppercase
    Result := Key;
    case Key of
      keyBS : Exit; // <BS>
      keyFSlash : Exit;  // </>
      key0..key9 : Exit; // [0..9]
      keyA..keyZ : Exit; // [A..Z]
      key_a..key_z : begin
                    Result := UpCase(Key);
                    Exit; // [a..z]
                  end;
    else
      Result := keyNull;
    end;// case Key of
end;// function ValidCallsignCharacter(var Key: char);

//========================================================================================
function ValidDigitCharacter( Key: char) : char;
begin
  // Returns only Valid Digits. Non-valid characters are converted
  // into Null (#0) characters.
  //Valid Digit Characters are:
  // <BS>
  // <DEL>
  // [0..9]
  Result := Key;
  case Key of
    keyBS : Exit; // <BS>
    key0..key9 : Exit; // [0..9]
  else
    Result := keyNull;
  end;// case Key of
end;// function ValidDigitCharacter(var Key: char);

//=======================================================================================
Function ValidFloatCharacter( Key: char) : char;
begin
  // Returns only Valid Digits and the Decimal Point. Non-valid characters are converted
  // into Null (keyNull) characters.
  // Valid Digit Characters are:
  // <BS>
  // [.]
  // <DEL>
  // [0..9]
  Result := Key;
  case Key of
    keyBS : Exit; // <BS>
    keyDecimalPoint : Exit; // <.>
    keyDEL : Exit; // <DEL>
    key0..key9 : Exit; // [0..9]
  else
    Result := keyNull;
  end;// case Key of
end;// Function ValidFloatCharacter( Key: char)

//========================================================================================
function ValidFilenameCharacter(Key: char) : char;
begin
    // Returns only Valid Filename Characters. Non-valid characters are converted
    // into Null (#0) characters.
    //Valid Alpha C haracters are:
    // <BS>
    // <SP>
    // [A..Z]
    // [a..z]
    // [0..9]
    // <_>
    Result := Key;
    case Key of
      keyBS : Exit; // <BS>
      keySpace : Exit; // <SP>
      key0..key9 : Exit; // [0..9]
      keyA..keyZ : Exit; // [A..Z]
      keyUScore : Exit; // <_>
      key_a..key_z : Exit; // [a..z]
    else
      Result := keyNull;
    end;// case Key of
end;// function ValidFilenameCharacter

//========================================================================================
function ValidDirectoryCharacter (Key: char) : char;
begin

    // Returns only Valid Directory Characters. Non-valid characters are converted
    // into Null (#0) characters.
    //Valid Alpha Characters are:
    // <BS>
    // <SP>
    // [A..Z]
    // [a..z]
    // [0..9]
    // <_>
    Result := Key;
    case Key of
      keyBS : Exit; // <BS>
      keySpace : Exit; // <SP>
      key0..key9 : Exit; // [0..9]
      keyA..keyZ : Exit; // [A..Z]
      keyUScore : Exit; // <_>
      key_a..key_z : Exit; // [a..z]
    else
      Result := keyNull;
    end;// case Key of

end;// function ValidDirectoryCharacter

//========================================================================================
function ValidPasswordCharacter (Key: char) : char;
begin

    // Returns only Valid Password Characters. Non-valid characters are converted
    // into Null (#0) characters.
    //Valid Password Characters are:
    // <BS>
    // <SP>
    // <Tab>
    // [A..Z]
    // [a..z]
    // [0..9]
    // keyDecimalPoint
    // <_>
    // keyFSlash
    Result := Key;
    case Key of
      keyBS : Exit; // <BS>
      keySpace : Exit; // <SP>
      keyTab : Exit;
      key0..key9 : Exit; // [0..9]
      keyA..keyZ : Exit; // [A..Z]
      keyUScore : Exit; // <_>
      key_a..key_z : Exit; // [a..z]
      keyDecimalPoint : Exit; // [.]
      keyFSlash : Exit; // [/];
    else
      Result := keyNull;
    end;// case Key of

end;// function ValidPasswordCharacter

//========================================================================================
//          Valid String Routines
//========================================================================================
procedure ValidRealStr (vstrValue: string; var vblnValid : Boolean);
var
  vintErrorCode : Integer;
  vrlTReal : Real;
  vintPos : Integer;
begin

  // Since we are looking for a Real number here, if there is no decimal point in the
  // string (Integer Value) we add '.0' to it to convert it into a Real value
  vintPos := Pos ('.', vstrValue);
  if vintPos = 0 then
    vstrValue := vstrValue+'.0';

  Val(vstrValue, vrlTReal, vintErrorCode);
  if vintErrorCode = 0 then
    vblnValid := True
  else
    vblnValid := False;
end;// function StrToReal (vstrValue)

//========================================================================================
//          CONVERSION ROUTINES
//========================================================================================
function HUBoolToStr( vblnBoolValue: boolean): string;
begin
  if vblnBoolValue = True then
    Result := 'TRUE'
  else
    Result := 'FALSE';
end;// function HUBoolToStr

//----------------------------------------------------------------------------------------
function HUStrToBool( vstrBoolStr: string): boolean;
begin
  if vstrBoolStr = 'TRUE' then
    Result := True
  else
    Result := False;
end;// function HUStrToBool

//========================================================================================
//          MESSAGES
//========================================================================================
function ErrorMessageDlgOk(vstrCaption, vstrMsg  : string) : TModalResult;
begin
  Result := MessageDlg(vstrCaption, vstrMsg, mtError, [mbOk], 0);
end;// function ErrorMessageDlgOk

//========================================================================================
function InfoMessageDlgOk(vstrCaption, vstrMsg  : string) : TModalResult;
begin
  Result := MessageDlg(vstrCaption, vstrMsg, mtInformation, [mbOk], 0);
end;// function InfoMessageDlgOk

//========================================================================================
function ConfirmationMessageDlg(vstrCaption, vstrMsg  : string) : TModalResult;
begin
  Result := MessageDlg(vstrCaption, vstrMsg, mtConfirmation, [mbYes, mbNo], 0);
end;// function ConfirmationMessageDlg

//========================================================================================
//          REGISTRATION ROUTINES
//========================================================================================
 function CalculateRegistrationKey (vstrInputString : string) : string;

 var
   vintVal1 : Longint;
   vintVal2 : Longint;
   vintVal3 : Longint;
   vintVal4 : Longint;
   vstrTStr : string;

 begin
   // A Registration Key is based on the Ordinal value of the first and last characters
   // of a string passed in the vstrInputString variable. These values are multiplied
   // five times to obtain at least a 10 digit integer. That integer is converted
   // into a string and the firt eight characters are returned as a calculated "Key"
   // value for that specific Input String.
   //
   // For Testing purposes, an input value of 'HU' will produce a Key of '93636000'
   // 'HS' will produce a Key of '89281440' and VU wil produce a Key of '13359025'.

   if Length(vstrInputString) < 2 then
   begin
     Result := '';
     Exit;
   end;// if Length() < 2

   vstrTStr := UpperCase(vstrInputString);
   vintVal1 := Ord(vstrTStr[1]);
   if vintVal1 < 32 then
     vintVal1 := 32;
   if vintVal1 > 90 then
     vintVal1 := 90;
   vintVal2 := Ord(vstrTStr[Length(vstrTStr)]);
   if vintVal2 < 32 then
     vintVal2 := 32;
   if vintVal2 > 90 then
     vintVal2 := 90;

   vintVal3 := vintVal1*vintVal2*vintVal1*vintVal2;
   vintVal4 := vintVal1*vintVal2*vintVal1*vintVal2;
   Result := Copy(IntToStr(vintVal3*25),1,8);

 end;// function ValidRegistration

//========================================================================================
//          DEBUGGING ROUTINES
//========================================================================================
procedure ShowInt(vstrTitle : string; vintValue : integer);
begin
  ShowMessage(' ' +
              vstrTitle +
              ' = ' +
              IntToStr(vintValue));
end;// procedure ShowInt

//========================================================================================
procedure ShowFloat(vstrTitle : string; vfltValue : double);
begin
  ShowMessage(' ' +
              vstrTitle +
              ' = ' +
              FloatToStrF(vfltValue, ffFixed, 5, 3));
end;// procedure ShowFloat

//========================================================================================
end.// unit HUtils;

